package com.asset.salesforce.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.asset.salesforce.domain.Appointment;
import com.asset.salesforce.domain.Call;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.Calendar;
import com.asset.salesforce.domain.Lead;
import com.asset.salesforce.form.EventElement;
import com.asset.salesforce.form.EventForm;
import com.asset.salesforce.service.AppointmentService;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.CallService;
import com.asset.salesforce.service.FileService;
import com.asset.salesforce.service.JoinRequestService;
import com.asset.salesforce.service.LeadService;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

	//import com.thoughtworks.xstream.XStream;
	//import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

@Controller
public class CalendarController {

	@Autowired
	private CalendarService calendarService;
	@Autowired
	private JoinRequestService joinRequestService;
	@Autowired
	private FileService fileService;
	@Autowired
	private LeadService leadService;
	@Autowired
	private CallService callService;
	@Autowired
	private AppointmentService appointmentService;
	
	@SuppressWarnings("deprecation")
	@RequestMapping(value="/calendar",method=RequestMethod.GET)
	public String loadCalendar(Model model,HttpSession session,HttpServletRequest request,
			@RequestParam(value="empId", required=false) Integer empId) {
		Employee userDetails=(Employee)session.getAttribute("userDetails");
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login";
		}
		Calendar cal = new Calendar();
		//List<Calendar> calendar = calendarService.getAllEvents(cal);
		List<Calendar> calendars = new ArrayList<Calendar>();
		calendars = calendarService.getEventsbyEmployeeId(empId);
	    
		String jsonEvents="empty";
		String jsonForm = new String(""); 
		EventForm eventForm = new EventForm();
		if(calendars != null){
			
			List<EventElement> eventElements = new ArrayList<EventElement>();
			for(Calendar calendar :calendars){
				
				java.util.Calendar calInst = java.util.Calendar.getInstance();
				EventElement element = new EventElement();
				element.setId(calendar.getId());
				if(calendar.getEventType().equals("call")){
					Call call = callService.getCallnById(new Call(), calendar.getParentId());
					if(call!=null){
						Lead leadCall = leadService.getLeadById(call.getParentId());
						element.setUrl("new-call?parentId="+leadCall.getId()+"&parentType=lead");
						if(calendar.getTitle()!=null&&leadCall!= null){
							//element.setTitle(calendar.getTitle() +" has made call for"+" Company Name:"+leadCall.getCompanyName());
							element.setTitle("call");
						}
						else{
							element.setTitle(calendar.getTitle());
						}
					}
				}else{
					if(calendar.getEventType().equals("appointment")){
						Appointment appointment = appointmentService.getAppointmentById(new Appointment(), calendar.getParentId());
						if(appointment!=null){
							Lead leadAppointment = leadService.getLeadById(appointment.getParentId());
							if(calendar.getIsFollowUp()!=null&&!calendar.getIsFollowUp().equalsIgnoreCase("")){
								element.setUrl("new-appointment?parentId="+leadAppointment.getId()+"&parentType=lead");
							}
							else{
								element.setUrl("view-appointment.html?id="+appointment.getId());
							}
							if(calendar.getTitle()!=null&&leadAppointment!= null){
								element.setTitle("appointment");
								//element.setTitle(calendar.getTitle() +"has made appointment for"+" Company Name: "+leadAppointment.getCompanyName());
							}
							else{
								element.setTitle(calendar.getTitle());
							}
						}
					}
				}
				
				calInst.setTime(calendar.getStartDate());
				String year;
				String month;
				String day;
				String hours;
				String mints;
				year =(new Integer(calInst.get(java.util.Calendar.YEAR))).toString();
				System.out.println(year);
				month =(new Integer(calInst.get(java.util.Calendar.MONTH))).toString();
				System.out.println(month);
				day = (new Integer(calInst.get(java.util.Calendar.DAY_OF_MONTH))).toString();
				System.out.println(day);
				hours =(new Integer(calInst.get(java.util.Calendar.HOUR_OF_DAY))).toString();
				System.out.println(hours);
				mints =(new Integer(calInst.get(java.util.Calendar.MINUTE))).toString();
				System.out.println(mints);
				
				
				element.setStart("new Date(" + year + "," + month + "," + day + "," + hours + "," + mints + ")");
				
				calInst.setTime(calendar.getEndDate());
				
				year =(new Integer(calInst.get(java.util.Calendar.YEAR))).toString();
				System.out.println(year);
				month =(new Integer(calInst.get(java.util.Calendar.MONTH))).toString();
				System.out.println(month);
				day = (new Integer(calInst.get(java.util.Calendar.DAY_OF_MONTH))).toString();
				System.out.println(day);
				hours =(new Integer(calInst.get(java.util.Calendar.HOUR_OF_DAY))).toString();
				System.out.println(hours);
				mints =(new Integer(calInst.get(java.util.Calendar.MINUTE))).toString();
				System.out.println(mints);
				element.setEnd("new Date(" + year + "," + month + "," + day + "," + hours + "," + mints + ")");
				if(element.getTitle().contains("call".toLowerCase())){
					element.setClassName("callcustom");
				}
				else{
					if(calendar.getIsFollowUp()!=null&&!calendar.getIsFollowUp().equalsIgnoreCase("")){
						if(calendar.getIsFollowUp().equals("true")){
							element.setClassName("appointmentfollowupcustom");
						}
					}
					else{
						element.setClassName("appointmentcustom");
					}
				}

				if(calendar.getEventType().equals("call")){
					Call call = callService.getCallnById(new Call(), calendar.getParentId());
					if(call!=null){
						Lead leadCall = leadService.getLeadById(call.getParentId());
						if(leadCall!= null){
							element.setTooltip("<div id='tooltip' class='hover-end'>"+calendar.getTitle() +" has made call for"+" Company Name:"+leadCall.getCompanyName()+"</div>");
						}
						else{
						}
					}
				}else{
					if(calendar.getEventType().equals("appointment")){
						Appointment appointment = appointmentService.getAppointmentById(new Appointment(), calendar.getParentId());
						if(appointment!=null){
							Lead leadAppointment = leadService.getLeadById(appointment.getParentId());
							if(leadAppointment!= null){
								element.setTooltip("<div id='tooltip' class='hover-end'>"+calendar.getTitle() +" has made appointment for"+" Company Name:"+leadAppointment.getCompanyName()+"</div>");
								//element.setTitle(calendar.getTitle() +"has made appointment for"+" Company Name: "+leadAppointment.getCompanyName());
							}
							else{
								element.setTitle(calendar.getTitle());
							}
						}
					}
				}
				element.setDescription(calendar.getDescription());
				element.setAllDay("false");
				eventElements.add(element);
				System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<one object"+element.toString());
			}
			
//			String jsonsEvents = 
//			System.out.println("???????????????????????????????????????????????????????"+jsonsEvents);
			eventForm.setEvents(eventElements);
//			XStream xStream1 = new XStream(new JettisonMappedXmlDriver());
//			xStream1.alias("jsonEvents", EventForm.class);
//			 xStream1.setMode(XStream.NO_REFERENCES);
//			 jsonForm = xStream1.toXML(eventForm);
			System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<all object"+eventForm.toString());
		}
//		if(!calendars.isEmpty()){
//			jsonEvents="{theme: true, header: { left: \"prev,next today\", center: \"title\", right: \"month,agendaWeek,agendaDay\" }, events : [";
//			java.util.Calendar calInst = java.util.Calendar.getInstance();
//
//			
//			for (Calendar calendarEvent : calendars) {
//				Integer leadId = calendarEvent.getParentId();			    
//			    Lead lead = leadService.getLeadById(leadId);
//				Integer year;
//				Integer month;
//				Integer day;
//				Integer hours;
//				Integer mints;
//				
//				jsonEvents+="{id: ";
//				jsonEvents+=calendarEvent.getId() + ", ";
//				
//				jsonEvents+="title: \"'";
////				jsonEvents+="<a href='/sales/s/viewLead.html?id="+lead.getId()+"'>"
////						+ lead.getfName()+" has: "+ calendarEvent.getEventType()+" by "+calendarEvent.getTitle()+"</a>"+"'\", ";
//				
//				jsonEvents+=calendarEvent.getTitle()+"created for"+ "lead name:" +lead.getfName()+ "\"', ";
//				
//				calInst.setTime(calendarEvent.getStartDate());
//				year =calInst.get(java.util.Calendar.YEAR);
//				System.out.println(year);
//				month =calInst.get(java.util.Calendar.MONTH);
//				System.out.println(month);
//				day =calInst.get(java.util.Calendar.DAY_OF_MONTH);
//				System.out.println(day);
//				hours =calInst.get(java.util.Calendar.HOUR_OF_DAY);
//				System.out.println(hours);
//				mints =calInst.get(java.util.Calendar.MINUTE);
//				System.out.println(mints);
//				jsonEvents+="start: ";
//				jsonEvents+="new Date(" + year + "," + month + "," + day + "," + hours + "," + mints + "), ";
//				
//				jsonEvents+="end: ";
//				calInst.setTime(calendarEvent.getEndDate());
//				year =calInst.get(java.util.Calendar.YEAR);
//				month =calInst.get(java.util.Calendar.MONTH);
//				day =calInst.get(java.util.Calendar.DAY_OF_MONTH);
//				hours =calInst.get(java.util.Calendar.HOUR_OF_DAY);
//				mints =calInst.get(java.util.Calendar.MINUTE);
//				jsonEvents+="new Date(" + year + "," + month + "," + day + "," + hours + "," + mints + "), ";
//				
//				//jsonEvents+="\"description\": \"";
//				//jsonEvents+=calendarEvent.getDescription() + "\", ";
//				
//				if(calendars.get((calendars.size())-1) == calendarEvent){
//					jsonEvents+="allDay: false }";
//				}
//				else{
//					jsonEvents+="allDay: false },";
//				}
//				System.out.println(jsonEvents + "<<<<<<<<&>>>>>>>>" + calendarEvent.getEndDate().getDate());
//			}
//			jsonEvents+="]}";
//			
//		}

//		else
//			if(calendars.size() <= 0){
//				return "redirect:/s/home";
//			}
		System.out.println("<<<<<<<<@>>>>>>>>>>>>>>>#<<<<<<<<<<<@>>>>>>>>" + eventForm.toString());
		model.addAttribute("userDetails", userDetails);
		model.addAttribute("jsonEvents", eventForm.toString());
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "calendar";
	}
	@SuppressWarnings("restriction")
	public static String encodeImage(byte[] imageByteArray) {
        return Base64.encode(imageByteArray);
    }
}
